package com.graduate.back.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.graduate.common.entity.Account;
import com.graduate.common.entity.Result;
import com.graduate.common.vo.AccountVo;
import com.graduate.common.vo.HandleVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author bwzfy
 * @create 2021/5/15
 **/
@Controller
@RequestMapping("/account")
public class AccountController extends BaseController {

    @ResponseBody
    @GetMapping("/list")
    public Result<IPage<AccountVo>> handleList(@RequestParam(required = false) Map<String, Object> map) {
        Result<IPage<AccountVo>> result = new Result<>();
        result.setCode(200);
        result.setData(accountService.selectAccountPage(map));
        return result;
    }

    @ResponseBody
    @GetMapping("/report")
    public void reportData(HttpServletResponse response) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException {
        List<Map<String, String>> rows = accountService.selectAllQuestions();
        // 需要list<map>类型的数据
        // 将查询出的数据传入到输出流中输出
        ExcelWriter writer = ExcelUtil.getWriter();
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=account.xls");
        ServletOutputStream outputStream = response.getOutputStream();
        writer.flush(outputStream, true);
        writer.close();
        IoUtil.close(outputStream);
    }

}
